<?php

namespace App\Service\Index\v1_3_0;

use App\Model\Act\ActRechargeLimit;
use App\Model\Act\HostTaskReward;
use App\Model\PayOrder;
use App\Model\UserChat;
use App\Model\UserProfile;
use App\Model\UserProfilePersonal;
use App\Service\BaseService;

/**
 * 弹窗基类
 */
class PopBaseService extends BaseService
{
    function packCommonData($title = '', $text = '', $btnText = '', $extra = [])
    {
        return array_merge([
            'title' => $title,
            'text' => $text,
            'button_text' => $btnText
        ], $extra);
    }

    /**
     * 充值优惠弹窗
     */
    function packRechargePop($userId = 0)
    {
        $profile = UserProfile::getProfile($userId);
        //女用户不显示优惠
        if (
            $profile->gender == 0
            || PayOrder::checkSuccessCount($userId, date('Y-m-d'), true)
        ) return [];
        //未充值
        if (!$payCount = PayOrder::checkSuccessCount($userId)) {
            $actConf = config('act.recharge.new');
            $actType = 1;
        } else {
            $actConf = config('act.recharge.old');
            $actType = 2;
            //今天刚充值首笔
            // $todayPay = PayOrder::checkSuccessCount($userId, date('Y-m-d'));
            //之前的老用户，且今天已经充值过，不弹窗
            // if ($payCount > 1 && $todayPay > 0) {
            //     return ['discount' => (string)$actConf['discount'], 'remain_time' => 0];
            // }
        }

        //加入活动充值记录
        ActRechargeLimit::addRecord($userId, $actConf['new_price'], $actConf['coin'], $actType);
        $remainTime = ActRechargeLimit::getBeginTime($userId, $actType) + $actConf['act_time'] - time();
        $remainTime = $remainTime > 0 ? $remainTime : 0;
        // if (!$remainTime) return ['discount' => (string)$actConf['discount']];
        $extra = [
            'button2_text' => "原价{$actConf['old_price']}元购买",
            'new_price' => (string)$actConf['new_price'],
            'old_price' => (string)$actConf['old_price'],
            'coin_text' => "{$actConf['coin']}",
            'remain_time' => $remainTime,
        ];
        $extra['discount'] = (string)$actConf['discount'];
        $data = $this->packCommonData('恭喜您获得限时充值优惠特权！', '', "限时{$actConf['new_price']}元领取", $extra);
        return $data;
    }

    /**
     * 主播任务进度
     */
    function checkHostTask($userId = 0, $profile = null)
    {
        $ret = [];
        $taskConf = config('act.host_task');
        $taskKeys = array_keys($taskConf);
        $taskInfo = HostTaskReward::getHostTask($userId);
        $checkAll = 1;
        foreach ($taskKeys as $taskType) {
            if (isset($taskInfo[$taskType])) {
                $ret[$taskType] = $taskInfo[$taskType] == HostTaskReward::STATUS_DONE ? 2 : 1;
            } else {
                $check = 0;
                switch ($taskType) {
                    case 'done_reg':
                        $check = 1;
                        break;
                    case 'real_person':
                        $personInfo = UserProfilePersonal::getProfile($userId);
                        $check = UserProfilePersonal::isFaceVerified($personInfo);
                        break;
                    case 'real_avatar':
                        $check = UserProfile::checkAvatarVrfied($userId, $profile);
                        break;
                    case 'fix_profile':
                        $check = UserProfile::checkProfileComplete($profile);
                        break;
                    case 'say_hi':
                        $check = UserChat::checkStartFrom($userId, UserChat::FROM_USER_LIST) ? 1 : 0;
                        break;
                    case 'msg_back':
                        $check = UserChat::checkMsgBack($userId) ? 1 : 0;
                        break;
                }
                if ($taskType != 'big_gift' && !$check) $checkAll = 0;
                if ($taskType == 'big_gift') $ret['big_gift'] = $checkAll;
                else $ret[$taskType] = $check;
                //任务完成入记录
                $check && HostTaskReward::addRecord($userId, $taskType, $taskConf[$taskType]['coin']);
            }
        }
        return $ret;
    }
}
